package com.digitalchina.lxnsy.activity.old;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.api.BusinessAccount;
import com.digitalchina.lxnsy.api.BusinessTechnologies;
import com.digitalchina.lxnsy.base.BaseActivity;
import com.digitalchina.lxnsy.constant.MSG;
import com.digitalchina.lxnsy.constant.URL;
import com.digitalchina.lxnsy.entity.ExperienceVO;
import com.digitalchina.lxnsy.entity.TechnologiesCommentVO;
import com.digitalchina.lxnsy.toolkit.FastJsonUtil;
import com.digitalchina.lxnsy.toolkit.FrescoUtil;
import com.digitalchina.lxnsy.toolkit.GotoUtil;
import com.digitalchina.lxnsy.toolkit.JointImageUrl;
import com.digitalchina.lxnsy.toolkit.StringUtil;
import com.digitalchina.lxnsy.toolkit.Utils;
import com.digitalchina.lxnsy.viewholder.ExpCommentHolder;
import com.digitalchina.lxnsy.xml.UserXML;
import com.facebook.drawee.view.SimpleDraweeView;
import com.jude.easyrecyclerview.EasyRecyclerView;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * ================================================
 * 项目名称：BigData
 * 类描述：
 * 包名：com.digitalchina.bigdata.activity
 * 创建人：郑皓
 * 创建时间：2019/4/17 18:54
 * 修改备注：
 * ================================================
 */
public class ExperienceDetailActivity extends BaseActivity implements RecyclerArrayAdapter.OnLoadMoreListener, SwipeRefreshLayout.OnRefreshListener {

    @Bind(R.id.recycler_view)
    EasyRecyclerView recyclerView;
    @Bind(R.id.et_msg)
    EditText etMsg;
    @Bind(R.id.tv_send)
    TextView tvSend;
    private ExperienceVO vo;

    private SimpleDraweeView sdv;
    private TextView tvName;
    private TextView tvAddress;
    private TextView tvTitle;
    private TextView tvContent;
    private TextView tvPraise;
    private ImageView ivPraise;
    private LinearLayout layoutPraise;
    private boolean isPraise = false;

    private RecyclerArrayAdapter adapter;
    private int page = 1;
    private int limit = 10;
    private List<TechnologiesCommentVO> voList = new ArrayList<>();
    private View headView;//头布局
    private String id = "";// 经验id

    @Override
    public void setLayout() {
        setContentLayout(R.layout.activity_experience_detail);
        setTitle("经验详情");
        setRightText("分享");
    }

    @Override
    public void initMember() {
        vo = (ExperienceVO) getIntent().getSerializableExtra("ExperienceVO");

        id = vo.getId();

        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        recyclerView.addItemDecoration(Utils.setSpaceDecoration(this, false, true, false));

        recyclerView.setAdapter(adapter = new RecyclerArrayAdapter(this) {
            @Override
            public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
                return new ExpCommentHolder(parent);
            }
        });


        headView = LayoutInflater.from(this).inflate(R.layout.headview_exp_detail, null);
        sdv = headView.findViewById(R.id.sdv);
        tvName = headView.findViewById(R.id.tv_name);
        tvAddress = headView.findViewById(R.id.tv_address);
        tvTitle = headView.findViewById(R.id.tv_title);
        tvContent = headView.findViewById(R.id.tv_content);
        tvPraise = headView.findViewById(R.id.tv_praise);
        ivPraise = headView.findViewById(R.id.iv_praise);
        layoutPraise = headView.findViewById(R.id.layout_praise);

        tvName.setText(vo.getUserName());
        tvAddress.setText(vo.getGeographicalPosition());
        tvTitle.setText(vo.getTitle());
        tvContent.setText(vo.getControlMethod());
        tvPraise.setText(vo.getPraiseNum() + "赞同");

        FrescoUtil.showImageSmall(vo.getImg(), sdv);


        adapter.addHeader(new RecyclerArrayAdapter.ItemView() {
            @Override
            public View onCreateView(ViewGroup parent) {

                return headView;
            }

            @Override
            public void onBindView(View headerView) {


            }
        });

        onRefresh();
    }

    @Override
    public void addListener() {
        recyclerView.setRefreshListener(this);

        adapter.setMore(R.layout.view_more, this);

        adapter.setNoMore(R.layout.view_nomore);

        adapter.setError(R.layout.view_error, new RecyclerArrayAdapter.OnErrorListener() {
            @Override
            public void onErrorShow() {
                adapter.resumeMore();
            }

            @Override
            public void onErrorClick() {
                adapter.resumeMore();
            }
        });

        tvSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (!"1".equals(UserXML.getLoginStatus(ExperienceDetailActivity.this))) {
                    GotoUtil.gotoActivity(ExperienceDetailActivity.this, LoginGuideActivity.class);
                    return;
                }

                if (StringUtil.isEditEmpty(etMsg)) {
                    showToast("请输入评论内容");
                    return;
                }

                BusinessTechnologies.publishComment(ExperienceDetailActivity.this, id, etMsg.getText().toString(), "8", mHandler);
            }
        });


        layoutPraise.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isPraise)
                    showToast("您已经赞过啦！");
                else {
                    BusinessAccount.appPraise(ExperienceDetailActivity.this, id, "11", vo.getTitle(), "", mHandler);
                }

            }
        });

    }

    @Override
    public void setHandler() {
        mHandler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {
                if (recyclerView == null)
                    return false;

                if (recyclerView.getSwipeToRefresh().isRefreshing())
                    recyclerView.setRefreshing(false);
                switch (msg.what) {
                    case MSG.MSG_GET_COMMENT_LIST_SUCCESS: //评论列表
                        callBack(msg.obj);
                        break;
                    case MSG.MSG_GET_COMMENT_LIST_FIELD:
                        if (adapter.getAllData().size() > 0)
                            adapter.pauseMore();
                        else
                            recyclerView.showEmpty();
                        break;
                    case MSG.MSG_PUBLISH_COMMENT_SUCCESS:  //发布评论
                        showToast("评论成功");
                        // 隐藏软键盘
                        InputMethodManager imm = (InputMethodManager) etMsg
                                .getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                        if (imm.isActive()) {
                            imm.hideSoftInputFromWindow(etMsg.getApplicationWindowToken(), 0);
                        }
                        etMsg.setText("");
                        onRefresh();
                        break;
                    case MSG.MSG_PUBLISH_COMMENT_FIELD:
                        showToast(msg.obj.toString());
                        break;
                    case MSG.MSG_PRAISE_SUCCESS: // 点赞
                        String s = FastJsonUtil.getString(msg.obj.toString(), "totalNum");
                        tvPraise.setText(s + "赞同");
                        ivPraise.setImageResource(R.drawable.icon_praise);
                        isPraise = true;
                        break;
                    case MSG.MSG_PRAISE_FIELD:
                        showToast(msg.obj.toString());
                        break;
                }
                return false;
            }
        });
    }

    @Override
    public void callBack(Object object) {
        voList = FastJsonUtil.getListBean(object.toString(), "body", "list", TechnologiesCommentVO.class);

        if (page == 1) { // 刷新
            adapter.clear();
        }

        adapter.addAll(voList);

        if (voList != null) {
            if (voList.size() < limit) {
                adapter.stopMore();
            }
        }
    }

    @Override
    public void rightOnClick() {


    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @Override
    public void onRefresh() {
        page = 1;
        BusinessTechnologies.getCommentList(this, id, "8", page, limit, mHandler);
    }

    @Override
    public void onLoadMore() {
        if (voList != null && voList.size() >= limit) {
            page++;
            BusinessTechnologies.getCommentList(this, id, "8", page, limit, mHandler);
        }
    }
}
